package com.sjz.han.industry.activites;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sjz.han.industry.R;
import com.sjz.han.industry.base.AppConfig;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.PicUtils;
import com.sjz.han.industry.wxapi.Util;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

//我的邀请码界面
public class MyYqmActivity extends BaseActivity implements View.OnClickListener{
    private String yqmString="";
    private ImageView yqm_photo_img;
    private TextView yqm_name_tv,yqm_one_tv,yqm_two_tv,yqm_three_tv,yqm_four_tv,yqm_five_tv,yqm_six_tv;
    private TextView yqm_share_tv;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_myyqm;
    }

    @Override
    protected void initDatas() {
        yqmString=getIntent().getStringExtra("YQM");
        PicUtils.showImageViewToCircle(this, R.mipmap.ic_wd_tx, ReqestUrl.BASE+shareUtils.getUserPhoto(), yqm_photo_img);
        yqm_name_tv.setText(shareUtils.getUserName());

        if(yqmString!=null&&yqmString.length()>=6){
            yqm_one_tv.setText(yqmString.charAt(0)+"");
            yqm_two_tv.setText(yqmString.charAt(1)+"");
            yqm_three_tv.setText(yqmString.charAt(2)+"");
            yqm_four_tv.setText(yqmString.charAt(3)+"");
            yqm_five_tv.setText(yqmString.charAt(4)+"");
            yqm_six_tv.setText(yqmString.charAt(5)+"");
        }

    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("我的邀请码");

        yqm_photo_img=(ImageView)this.findViewById(R.id.yqm_photo_img);

        yqm_name_tv=(TextView)this.findViewById(R.id.yqm_name_tv);
                yqm_one_tv=(TextView)this.findViewById(R.id.yqm_one_tv);
                yqm_two_tv=(TextView)this.findViewById(R.id.yqm_two_tv);
                yqm_three_tv=(TextView)this.findViewById(R.id.yqm_three_tv);
                yqm_four_tv=(TextView)this.findViewById(R.id.yqm_four_tv);
                yqm_five_tv=(TextView)this.findViewById(R.id.yqm_five_tv);
                yqm_six_tv=(TextView)this.findViewById(R.id.yqm_six_tv);

        yqm_share_tv=(TextView)this.findViewById(R.id.yqm_share_tv);
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        yqm_share_tv.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
            case R.id.yqm_share_tv:
                wechatShare(0);
                break;
        }
    }

    //===========================微信分享======================================
    private String shareUrl="";
    private String shareTitle="INDUSTRY";
    private String shareSummary="奔向财富自由之路！";
    private void wechatShare(int flag) {//0微信，1朋友圈
        shareUrl=ReqestUrl.BASEH5+"/frontHtml/yindao.php?a="+shareUtils.getYQM();
        IWXAPI api = WXAPIFactory.createWXAPI(this, AppConfig.app_id);
        api.registerApp(AppConfig.app_id);
        if (!api.isWXAppInstalled()) {
            Toast.makeText(this, "您还未安装微信客户端",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareUrl;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = shareTitle;
        msg.description = shareSummary;
        //这里替换一张自己工程里的图片资源
        Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }
}
